<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="author" content="JinTaoOO">
    
    
    
    
    
    
    <title>fabric 高级画板 | JinTaoOO</title>
    <link href="http://example.com" rel="prefetch" />

    
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/aos.css">
<link rel="stylesheet" href="/css/style.css">

    
<script src="/js/jquery.min.js"></script>

    
<script src="/js/bootstrap.min.js"></script>

    
<script src="/js/aos.js"></script>

    
<script src="/js/highslide/highslide-full.min.js"></script>

    
<link rel="stylesheet" href="/js/highslide/highslide.css">

    <style type="text/css">
        @media (max-width: 768px) {
            body {
                background-color: #f0f0f0;
                background: url('/imgs/xsbg.gif');
                background-attachment: fixed;
            }
        }
    </style>
    
    <!--<script type="text/javascript">
      if (document.images) {
        var avatar = new Image();
        avatar.src = '/imgs/avatar.jpg'
        var previews = 'preview1.jpg,preview2.jpg,preview3.jpg,preview4.jpg'.split(',')
        var previewsPreLoad = []
        for(var i = 0; i < length; i++) {
          previewsPreLoad.push(new Image())
          previewsPreLoad[previewsPreLoad.length - 1].src = '/imgs/preview' + previews[i]
        }
      }
    </script>-->
<meta name="generator" content="Hexo 5.3.0"></head>
<body>
    <!-- 背景轮播图功能 -->
    <section class="hidden-xs">
    <ul class="cb-slideshow">
        <li><span>天若</span></li>
        <li><span>有情</span></li>
        <li><span>天亦老</span></li>
        <li><span>我为</span></li>
        <li><span>长者</span></li>
        <li><span>续一秒</span></li>
    </ul>
</section>
    <!-- 欧尼酱功能, 谁用谁知道 -->
    
    <header class="navbar navbar-inverse" id="gal-header">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed"
                    data-toggle="collapse" data-target=".bs-navbar-collapse"
                    aria-expanded="false">
                <span class="fa fa-lg fa-reorder"></span>
            </button>
            <a href="http://example.com">
                
                <style>
                    #gal-header .navbar-brand {
                        height: 54px;
                        line-height: 24px;
                        font-size: 28px;
                        opacity: 1;
                        background-color: rgba(0,0,0,0);
                        text-shadow: 0 0 5px #fff,0 0 10px #fff,0 0 15px #fff,0 0 20px #228DFF,0 0 35px #228DFF,0 0 40px #228DFF,0 0 50px #228DFF,0 0 75px #228DFF;
                    }
                </style>
                <!-- 这里使用文字(navbar_text or config.title) -->
                <div class="navbar-brand">JinTaoOO</div>
                
            </a>
        </div>
        <div class="collapse navbar-collapse bs-navbar-collapse">
            <ul class="nav navbar-nav" id="menu-gal">
                
                
                <li class="">
                    <a href="/">
                        <i class="fa fa-home"></i>首页
                    </a>
                </li>
                
                
                
                <li class="">
                    <a href="/archives">
                        <i class="fa fa-archive"></i>归档
                    </a>
                </li>
                
                
                
                
                <li class="">
                    <a href="/categories">
                        <i class="fa fa-list"></i>分类
                    </a>
                </li>
                
                
                
                
                
                <li class="">
                    <a href="/tags">
                        <i class="fa fa-tags"></i>标签
                    </a>
                </li>
                
                
                
                
                <li class="">
                    <a href="/about">
                        <i class="fa fa-user"></i>关于我
                    </a>
                </li>
                
                
            </ul>
        </div>
    </div>
</header>
    <div id="gal-body">
        <div class="container">
            <div class="row">
                <div class="col-md-8 gal-right" id="mainstay">
                    
<article class="article well article-body" id="article">
    <div class="breadcrumb">
        <i class="fa fa-home"></i>
        <a href="http://example.com">JinTaoOO</a>
        >
        <span>fabric 高级画板</span>
    </div>
    <!-- 大型设备详细文章 -->
    <div class="hidden-xs">
        <div class="title-article">
            <h1>
                <a href="/2019/04/07/fabric/">fabric 高级画板</a>
            </h1>
        </div>
        <div class="tag-article">
            
            <span class="label label-gal">
                <i class="fa fa-calendar"></i> 2019-04-07
            </span>
            
        </div>
    </div>
    <!-- 小型设备详细文章 -->
    <div class="visible-xs">
        <center>
            <div class="title-article">
                <h4>
                    <a href="/2019/04/07/fabric/">fabric 高级画板</a>
                </h4>
            </div>
            <p>
                <i class="fa fa-calendar"></i> 2019-04-07
            </p>
            <p>
                
                
            </p>
        </center>
    </div>
    <div class="content-article">
        <p>fabric 一款封装 canvas 方法的 js, 可将 canvas 中的元素对象化， 轻松实现元素拖拽 放大缩小。</p>
<h3 id="开始"><a href="#开始" class="headerlink" title="开始"></a>开始</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">&quot;https://cdn.bootcss.com/fabric.js/2.4.0/fabric.min.js&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line"><span class="comment">// 通过cdn 引入 fabric.js</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> canvas = (<span class="built_in">this</span>.__canvas = <span class="keyword">new</span> fabric.Canvas(<span class="string">&quot;c&quot;</span>, &#123;</span><br><span class="line">  isDrawingMode: <span class="literal">true</span>, <span class="comment">// 自由绘画</span></span><br><span class="line">  selection: <span class="literal">false</span>, <span class="comment">// 元素是否可多选</span></span><br><span class="line">  width: <span class="number">100</span>, <span class="comment">// canvas 画布宽高度</span></span><br><span class="line">  height: <span class="number">100</span>,</span><br><span class="line">&#125;));</span><br></pre></td></tr></table></figure>
<p>到到这一步已经实现了自由绘画 且绘画元素支持拖拽，缩放，旋转 等功能</p>
<h3 id="升阶用法"><a href="#升阶用法" class="headerlink" title="升阶用法"></a>升阶用法</h3><ol>
<li>toJSon</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> canvasJSON = canvas.toJSON(); <span class="comment">// 将canvas 元素信息已对象形式存储</span></span><br><span class="line">canvas.loadFromDatalessJSON(canvasJSON); <span class="comment">// 将 对象写入画布</span></span><br></pre></td></tr></table></figure>
<p>用途: 可保存当次的操作存入数据库 二次进入无需重头开始</p>
<ol start="2">
<li>setBackgroundImage 设置背景图片 背景图片不会被选取修改属性</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">canvas.setBackgroundImage(</span><br><span class="line">  <span class="string">&quot;http://fabricjs.com/assets/dragon2.jpg&quot;</span>,</span><br><span class="line">  canvas.renderAll.bind(canvas),</span><br><span class="line">  &#123;</span><br><span class="line">    crossOrigin: <span class="string">&quot;Anonymous&quot;</span>, <span class="comment">// 图片跨域问题</span></span><br><span class="line">    scaleX: canvas.width / imgW,</span><br><span class="line">    scaleY: canvas.height / imgH,</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h3 id="常用对象和属性"><a href="#常用对象和属性" class="headerlink" title="常用对象和属性"></a>常用对象和属性</h3><ol>
<li><p>对象(绘画方法)<br>fabric.Circle 圆<br>fabric.Ellipse 椭圆<br>fabric.Line 直线<br>fabric.Polygon 多边形<br>fabric.Polyline<br>fabric.Rect 矩形<br>fabric.Triangle 三角形</p>
</li>
<li><p>方法<br>add(object) 添加<br>remove(object) 移除<br>getObjects() 获取所有对象<br>item(int) 获取子项<br>isEmpty() 判断是否空画板<br>size() 画板元素个数<br>contains(object) 查询是否包含某个元素<br>clear() 清空<br>renderAll() 重绘<br>requestRenderAll() 请求重新渲染<br>rendercanvas() 重绘画板<br>toDatalessJSON() 画板信息序列化成最小的 json<br>toJSON() 画板信息序列化成 json<br>setCursor() 设置手势图标<br>getSelectionContext()获取选中的 context<br>getSelectionElement()获取选中的元素<br>getActiveObject() 获取选中的对象<br>getActiveObjects() 获取选中的多个对象<br>discardActiveObject()取消当前选中对象<br>rotate() 设置旋转角度</p>
</li>
<li><p>事件<br>canvas.on(“mouse:down”, () =&gt; {}); // 按下<br>canvas.on(“mouse:move”, () =&gt; {}); // 移动<br>canvas.on(“mouse:up”, () =&gt; {}); // 松开<br>canvas.on(“mouse:dblclick”, () =&gt; {}); // 双击</p>
</li>
</ol>
<h3 id="demo"><a href="#demo" class="headerlink" title="demo"></a>demo</h3><ol>
<li><p>demo网页 <a target="_blank" rel="noopener" href="http://jintaoo.gitee.io/fabric_canvas" title="点击前往">http://jintaoo.gitee.io/fabric_canvas</a></p>
</li>
<li><p>代码地址 <a target="_blank" rel="noopener" href="https://gitee.com/jintaOO/fabric_canvas.git" title="点击前往">https://gitee.com/jintaOO/fabric_canvas.git</a></p>
</li>
</ol>

    </div>
</article>


                </div>
                <aside class="col-md-4 gal-left" id="sidebar">
    <!-- 此为sidebar的搜索框, 非搜索结果页面 -->
<aside id="sidebar-search">
    <div class="search hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <form class="form-inline clearfix" id="search-form" method="get"
              action="/search/index.html">
            <input type="text" name="s" class="form-control" id="searchInput" placeholder="搜索文章~" autocomplete="off">
            <button class="btn btn-danger btn-gal" type="submit">
                <i class="fa fa-search"></i>
            </button>
        </form>
    </div>
</aside>
    <aside id="sidebar-author">
    <div class="panel panel-gal" data-aos="flip-right" data-aos-duration="3000">
        <div class="panel-heading" style="text-align: center">
            <i class="fa fa-quote-left"></i>
            JinTaoOO
            <i class="fa fa-quote-right"></i>
        </div>
        <div class="author-panel text-center">
            <img src="/imgs/avatar.jpg" width="140" height="140"
                 alt="个人头像" class="author-image">
            <p class="author-description"></p>
        </div>
    </div>
</aside>
    
    <aside id="sidebar-recent_comments">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-comments"></i>
            最新评论
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush"></ul>
    </div>
</aside>
    
    <!-- 要配置好leancloud才能开启此小工具 -->
    
    
    <aside id="sidebar-recent_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            近期文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/webSocket/">webSocket实现多人你画我猜</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/node-mySql/">使用 Node.js + Express+MySQL 实现简单的增删改查</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/node-mongodb/">Node 查询 mongodb 优化工具 redis</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/fabric/">fabric 高级画板</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="sidebar-rand_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            随机文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/node-mongodb/">Node 查询 mongodb 优化工具 redis</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/node-mySql/">使用 Node.js + Express+MySQL 实现简单的增删改查</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/webSocket/">webSocket实现多人你画我猜</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/04/07/fabric/">fabric 高级画板</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="gal-sets">
        <div class="panel panel-gal hidden-xs" data-aos="fade-up" data-aos-duration="2000">
            <ul class="nav nav-pills pills-gal">

                
                <li>
                    <a href="/2019/04/07/fabric/index.html#sidebar-tags" data-toggle="tab" id="tags-tab">热门标签</a>
                </li>
                
                
                <li>
                    <a href="/2019/04/07/fabric/index.html#sidebar-friend-links" data-toggle="tab" id="friend-links-tab">友情链接</a>
                </li>
                
                
                <li>
                    <a href="/2019/04/07/fabric/index.html#sidebar-links" data-toggle="tab" id="links-tab">个人链接</a>
                </li>
                
            </ul>
            <div class="tab-content">
                
                <div class="cloud-tags tab-pane nav bs-sidenav fade" id="sidebar-tags">
    
</div>
                
                
                <div class="friend-links tab-pane nav bs-sidenav fade" id="sidebar-friend-links">
    
    <li>
        <a href="https://juejin.cn/" target="_blank">掘金</a>
    </li>
    
</div>
                
                
                <div class="links tab-pane nav bs-sidenav fade" id="sidebar-links">
    
    <li>
        <a href="https://github.com/JinTaoOO" target="_blank">Github</a>
    </li>
    
    <li>
        <a href="https://gitee.com/jintaOO" target="_blank">码云</a>
    </li>
    
    <li>
        <a href="jintaooo@139.com" target="_blank">邮箱</a>
    </li>
    
</div>
                
            </div>
        </div>
    </aside>
    
</aside>
            </div>
        </div>
    </div>
    <footer id="gal-footer">
    <div class="container">
        Copyright © 2018 JinTaoOO Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>.&nbsp;Theme by <a href="https://github.com/ZEROKISEKI" target="_blank">AONOSORA</a>
    </div>
</footer>

<!-- 回到顶端 -->
<div id="gal-gotop">
    <i class="fa fa-angle-up"></i>
</div>
</body>

<script src="/js/activate-power-mode.js"></script>

<script>

    // 配置highslide
	hs.graphicsDir = '/js/highslide/graphics/'
    hs.outlineType = "rounded-white";
    hs.dimmingOpacity = 0.8;
    hs.outlineWhileAnimating = true;
    hs.showCredits = false;
    hs.captionEval = "this.thumb.alt";
    hs.numberPosition = "caption";
    hs.align = "center";
    hs.transitions = ["expand", "crossfade"];
    hs.lang.number = '共%2张图, 当前是第%1张';
    hs.addSlideshow({
      interval: 5000,
      repeat: true,
      useControls: true,
      fixedControls: "fit",
      overlayOptions: {
        opacity: 0.75,
        position: "bottom center",
        hideOnMouseOut: true
      }
    })

    // 初始化aos
    AOS.init({
      duration: 1000,
      delay: 0,
      easing: 'ease-out-back'
    });

</script>
<script>
	POWERMODE.colorful = 'true';    // make power mode colorful
	POWERMODE.shake = 'true';       // turn off shake
	// TODO 这里根据具体情况修改
	document.body.addEventListener('input', POWERMODE);
</script>
<script>
    window.slideConfig = {
      prefix: '/imgs/slide/background',
      ext: 'jpg',
      maxCount: '6'
    }
</script>

<script src="/js/hs.js"></script>
<script src="/js/blog.js"></script>




</html>